<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <new-page-filter
      v-model:query="filterInfo.query"
      :clear-type="clearType"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
    >
      <template v-slot:filter-code>
        <div class="scan-input-box">
          <el-input
            ref="codeInput"
            v-model="filterInfo.query.code"
            v-focus
            :clearable="true"
            style="width: 400px"
            placeholder="请扫码或输入发货单二维码"
            @keyup.enter.native="sacn"
            @input="(e) => (code = validSe(e))"
          >
            <template #prepend>发货单二维码</template>
          </el-input>
        </div>
      </template>
    </new-page-filter>
    <!-- 条件栏 -->
    <table-container
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      :api="tableInfo.api"
      :query="filterInfo.query"
      v-model:data="tableInfo.data"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="checkBox"
      :isDynamic="false"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-qty="scope">
        {{ $fn.Subtr(scope.row.qty, scope.row.receiveQty || 0) }}
      </template>
    </table-container>
  </page-dialog>
</template>
<script setup name="addBydeliverOrder">
import { getSupplierAll } from '@/api/core'
import { getPendReceivingDeliverOrderList } from '@/api/supplier'
import { receivingsScanDeliverCode } from '@/api/stock'

const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: true,
  },
  checkBox: {
    type: Boolean,
    default: false,
  },
  visible: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
})

// 过滤相关配置
const filterInfo = ref({
  copyQuery: {},
  query: {
    statusMult: '2,3,5',
    deliverProductType: '2',
    supplierAuth: 'supplierId',
  },
  searchList: [
    { slot: true, label: '请扫码', value: 'code' },
    {
      componentType: 'input',
      label: '发货单号',
      value: 'orderNo',
      event: search,
    },
    {
      componentType: 'input',
      label: '采购单号',
      value: 'purchNo',
      event: search,
    },
    {
      componentType: '供应商',
      value: 'supplierIdMult',
      type: 'slip-select',
      api: getSupplierAll,
      query: { supplierAuth: 'supplierId', sort: 'create_time desc' },
      clearable: true,
      filterable: true,
      keyField: 'supplierName',
      valueField: 'supplierId',
      event: search,
    },
  ],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
})
const clearType = ref(true)
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: getPendReceivingDeliverOrderList,
  fieldList: [
    { label: '发货单号', value: 'orderNo' },
    { label: '采购单号', value: 'purchNo' },
    { label: '供应商', value: 'supplierName' },
    { label: '状态', value: 'statusValue' },
    { label: '备注', value: 'remark' },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: '未完成订单',
  width: '80%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    { label: '保存', event: handleSave, disabled: true, show: true },
  ],
})
const selectRow = ref({})

const shows = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件visible
  },
})

setData()
function setData() {
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
  getList()
}
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function sacn() {
  if (!filterInfo.value.query.code) {
    proxy.$message({
      message: '请扫码',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  }
  receivingsScanDeliverCode(encodeURIComponent(filterInfo.value.query.code))
    .then((res) => {
      if (res && res.success) {
        shows.value = false
        emits('callback', res.data)
      } else {
        filterInfo.value.query.code = ''
      }
    })
    .catch(() => {
      filterInfo.value.query.code = ''
    })
}
function handleEvent(event, data) {
  if (event === 'tableClickRow') {
    if (data && data.id) {
      selectRow.value = data
      dialogInfo.value.btList[1].disabled = false
    }
  }
}
function handleSave() {
  shows.value = false
  emits('callback', selectRow.value)
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
}
function clearSearch() {
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  clearType.value = !clearType.value
  getList()
}
</script>
<style scoped lang="scss"></style>
